#!/bin/sh

set -e

# http://erlang.org/doc/man/run_erl.html
# If defined, disables input and output flow control for the pty
# opend by run_erl. Useful if you want to remove any risk of accidentally
# blocking the flow control by using Ctrl-S (instead of Ctrl-D to detach),
# which can result in blocking of the entire Beam process, and in the case
# of running heart as supervisor even the heart process becomes blocked
# when writing log message to terminal, leaving the heart process unable
# to do its work.
RUN_ERL_DISABLE_FLOWCNTRL=${RUN_ERL_DISABLE_FLOWCNTRL:-true}
export RUN_ERL_DISABLE_FLOWCNTRL

if [ "$TERM" = "dumb" ] || [ -z "$TERM" ]; then
  export TERM=screen
fi

# OSX does not support readlink '-f' flag, work
# around that
# shellcheck disable=SC2039,SC3000-SC4000
case $OSTYPE in
    darwin*)
        SCRIPT=$(readlink "$0" || true)
    ;;
    *)
        SCRIPT=$(readlink -f "$0" || true)
    ;;
esac
[ -z "$SCRIPT" ] && SCRIPT=$0
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT")" && pwd -P)"
RELEASE_ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd -P)"
# Make the value available to variable substitution calls below
export REL_NAME="{{ release_name }}"
REL_VSN="{{ release_version }}"

# export these to match mix release environment variables
export RELEASE_NAME="{{ release_name }}"
export RELEASE_VSN="{{ release_version }}"
export RELEASE_PROG="${SCRIPT}"

ERTS_VSN="{{ erts_vsn }}"
REL_DIR="$RELEASE_ROOT_DIR/releases/$REL_VSN"
RUNNER_LOG_DIR="${RUNNER_LOG_DIR:-$RELEASE_ROOT_DIR/log}"
export ESCRIPT_NAME="${ESCRIPT_NAME-$SCRIPT}"


# if RELX_RPC_TIMEOUT is set then use that
# otherwise check for NODETOOL_TIMEOUT and convert to seconds
if [ -z "$RELX_RPC_TIMEOUT" ]; then
    # if NODETOOL_TIMEOUT exists then turn the old nodetool timeout into the rpc timeout
    if [ -n "$NODETOOL_TIMEOUT" ]; then
        # will exit the script if NODETOOL_TIMEOUT isn't a number
        RELX_RPC_TIMEOUT=$((NODETOOL_TIMEOUT / 1000))
    else
        RELX_RPC_TIMEOUT=60
    fi
fi

export RELX_RPC_TIMEOUT

# start/stop/install/upgrade pre/post hooks
PRE_START_HOOKS="hooks/pre_start.sh"
POST_START_HOOKS=""
PRE_STOP_HOOKS=""
POST_STOP_HOOKS=""
EXTENSIONS="{{{ extensions }}}"

relx_usage() {
    command="$1"

    case "$command" in
        foreground)
            echo "Usage: $REL_NAME foreground [--config aeternity.{json|yaml}]"
            echo "  Starts the Aeternity node in the foreground, meaning all output"
            echo "  going to stdout but without an interactive shell."
            echo "  This command is recommended for starting the Aeternity node"
            echo "  in a systemd based system as a simple process."
            ;;
        start)
            echo "Usage: $REL_NAME start  [--config aeternity.{json|yaml}]"
            echo "  Starts the Aeternity node as a background daemon"
            echo "  This command is provided for use in traditional SysV init scripts"
            ;;
        console)
            echo "Usage: $REL_NAME console  [--config aeternity.{json|yaml}]"
            echo "  Starts the Aeternity node with an interactive Erlang shell."
            ;;
        cli)
            echo "Usage: $REL_NAME cli"
            echo "  Open a cli towards the Aeternity node."
            ;;
        remote)
            echo "Usage: $REL_NAME remote"
            echo "  Attach a remote Erlang shell to an already running Aeternity node."
            ;;
        stop)
            echo "Usage: $REL_NAME stop"
            echo "  Cleanly stop the running Aeternity node."
            ;;
        restart)
            echo "Usage: $REL_NAME restart"
            echo "  Restart the internal processes of the running Aeternity node"
            echo "  without restarting the OS process."
            ;;
        pid)
            echo "Usage: $REL_NAME pid"
            echo "  Print the OS pid of the running Aeternity node."
            ;;
        ping)
            echo "Usage: $REL_NAME ping"
            echo "  Test if the Aeternity node is running and responsive."
            ;;
        status)
            echo "Usage: $REL_NAME status"
            echo "  Show summary information about the Aeternity node."
            ;;
        check_config)
            echo "Usage: $REL_NAME check_config CONFIG_FILE [SCHEMA_FILE]"
            echo "  Validate the aeternity config file against its schema."
            ;;
        keys_gen)
            echo "Usage: $REL_NAME keys_gen PASSWORD"
            echo "  Generate a password protected public private keypair."
            ;;
        peer_key)
            echo "Usage: $REL_NAME peer_key"
            echo "  Show the peer key of the Aeternity node."
            echo "  This key is presented to other nodes and can be used by other nodes"
            echo "  to connect to this node."
            ;;
        create_whitelist)
            echo "Usage: $REL_NAME create_whitelist [-start Start] [-n N] [-step Step] [-o Outfile]"
            echo "  -start Start : Begin <Start> key blocks below the current top (default: 100)"
            echo "  -n     N     : Collect <N> hashes for the whitelist (default: 1000)"
            echo "  -step  Step  : Separate the whitelisted hashes by <Step> (default: 1)"
            echo "  -outfile F   : Write JSON to file <F> (default: ./block_whitelist-X-Y-Z.json)"
            ;;
        db_rollback)
            echo "Usage: $REL_NAME db_rollback [-h Height] [-b Blockhash] [-w Whitelist]"
            echo "  Rollback the blockchain to the specified height, blockhash, or to a whilelist."
            echo "  -h (-height)    : Chain height from which to start deleting"
            echo "  -n (-blockhash) : Block hash from which to start deleting"
            echo "  -w (-whitelist) : Roll back based on contents of whitelist (true | false)"
            ;;
        db_migrate)
            echo "Usage: $REL_NAME db_migrate [-s Silent]"
            echo "  Migrate rocksdb storage created before version 6.5.2 to use higher performance column families"
            echo "  This can take a few hours and the node will be placed in maintenance mode during the migration"
            echo "  -s (-silent)    : Suppress progress reports"
            ;;
        db_safe_access_scan)
            echo "Usage: $REL_NAME db_safe_access_scan [-t Tree]"
            echo "  Scan state tree DB for incorrect/tainted values - useful when running on an untrusted DB snapshot"
            echo "  -t (--tree)  : State tree to scan, default all (accounts, calls, contracts, oracles, channels, ns)"
            ;;
        maintenance)
            echo "Usage: $REL_NAME maintenance [on | off | status] [-t timeout]"
            echo "  Enable / disable / check status of maintenance mode"
            echo "  Maintenance mode disables mining, sync and HTTP endpoints"
            echo "  It is useful when performing operations on the system where the database"
            echo "  should not change, and other debugging / maintenance tasks"
            echo "  -t (-timeout) : Set the max time this command can wait until the mode change is done (millisecs)"
            ;;
        offline)
            echo "Usage: $REL_NAME offline [on | off | status] [-t timeout]"
            echo "  Enable / disable / check status of offline mode"
            echo "  Offline mode disables mining and sync, but leaves HTTP endpoints active"
            echo "  It is useful for testing a node in isolation"
            echo "  -t (-timeout) : Set the max time this command can wait until the mode change is done (millisecs)"
            ;;
        admin)
            echo "Usage: $REL_NAME admin [SUB COMMAND]"
            echo "  Additional Commands to administer the system"
            echo "  To get further info just run $REL_NAME admin"
            ;;
        help)
            echo "Usage: $REL_NAME help [COMMAND]"
            echo "  Get additional help about a command"
            ;;
        *)
            # check for extension
            IS_EXTENSION=$(relx_is_extension "$command")
            if [ "$IS_EXTENSION" = "1" ]; then
                EXTENSION_SCRIPT=$(relx_get_extension_script "$command")
                relx_run_extension "$EXTENSION_SCRIPT" help
            else
                echo "Usage: ${REL_NAME} [COMMAND] [ARGS]

Commands:

  foreground              Start aeternity node with output to stdout
  start                   Start aeternity node as a background process
  remote                  Connect remote Erlang shell to running aeternity node
  stop                    Stop the running node
  restart                 Restart the internal applications but not the OS process
  cli                     Open a CLI towards the running aeternity node
  pid                     Print the PID of the OS process
  ping                    Print pong if the aeternity node is alive
  console                 Start the aeternity node in the foreground with an interactive Erlang shell
  status                  Verify node is running and print status summary
  version                 Print node version
  check_config            Check validity of a configuration file
  keys_gen                Generate password protected account keypair
  peer_key                Display the peer key of the aeternity node
  create_whitelist        Export block hashes within a range of heights to json file
  db_rollback             Rollback blockchain to specified height, hash, or from whitelist file
  db_migrate              Migrate rocksdb database to column families
  db_safe_access_scan     Run safe access scan for state tree DB(s)
  maintenance             Enable / disable maintenance mode
  offline                 Enable / disable offline mode
  admin                   Aeternity node administration commands
  help                    Get additional help about a command
  "
            fi
            ;;
    esac
}

find_erts_dir() {
    __erts_dir="$RELEASE_ROOT_DIR/erts-$ERTS_VSN"
    if [ -d "$__erts_dir" ]; then
        ERTS_DIR="$__erts_dir";
    else
        __erl="$(command -v erl)"
        code="io:format(\"~s\", [code:root_dir()]), halt()."
        __erl_root="$("$__erl" -boot no_dot_erlang -sasl errlog_type error -noshell -eval "$code")"
        ERTS_DIR="$__erl_root/erts-$ERTS_VSN"
        if [ ! -d "$ERTS_DIR" ]; then
            erts_version_code="io:format(\"~s\", [erlang:system_info(version)]), halt()."
            __erts_version="$("$__erl" -boot no_dot_erlang -sasl errlog_type error -noshell -eval "$erts_version_code")"
            ERTS_DIR="${__erl_root}/erts-${__erts_version}"
            if [ -d "$ERTS_DIR" ]; then
                echo "Exact ERTS version (${ERTS_VSN}) match not found, instead using ${__erts_version}. The release may fail to run." 1>&2
                ERTS_VSN=${__erts_version}
            else
                echo "Can not run the release. There is no ERTS bundled with the release or found on the system."
                exit 1
            fi
        fi
    fi
}

find_erl_call() {
    # users who depend on stdout when running rpc calls must still use nodetool
    # so we have an overload option to force use of nodetool instead of erl_call
    if [ "$USE_NODETOOL" ]; then
        ERL_RPC=relx_nodetool
    else
        # only OTP-23 and above have erl_call in the erts bin directory
        # and only those versions have the features and bug fixes needed
        # to work properly with this script
        __erl_call="$ERTS_DIR/bin/erl_call"
        if [ -f "$__erl_call" ]; then
            ERL_RPC="$__erl_call";
        else
            ERL_RPC=relx_nodetool
        fi
    fi
}

# Get node pid
relx_get_pid() {
    if output="$(erl_rpc os getpid 2>/dev/null)"
    then
        echo "$output" | sed -e 's/"//g'
        return 0
    else
        echo "$output"
        return 1
    fi
}

ping_or_exit() {
    if ! erl_rpc erlang is_alive > /dev/null 2>&1; then
        echo "Node is not running!"
        exit 1
    fi
}

relx_get_nodename() {
    id="longname$(relx_gen_id)-${NAME}"
    if [ -z "$COOKIE" ]; then
        # shellcheck disable=SC2086
        "$BINDIR/erl" -boot "$REL_DIR"/start_clean \
                      -mode interactive \
                      -boot_var SYSTEM_LIB_DIR "$SYSTEM_LIB_DIR" \
                      -eval '[_,H]=re:split(atom_to_list(node()),"@",[unicode,{return,list}]), io:format("~s~n",[H]), halt()' \
                      -dist_listen false \
                      ${START_EPMD} \
                      -noshell "${NAME_TYPE}" "$id"
    else
        # running with setcookie prevents a ~/.erlang.cookie from being created
        # shellcheck disable=SC2086
        "$BINDIR/erl" -boot "$REL_DIR"/start_clean \
                      -mode interactive \
                      -boot_var SYSTEM_LIB_DIR "$SYSTEM_LIB_DIR" \
                      -eval '[_,H]=re:split(atom_to_list(node()),"@",[unicode,{return,list}]), io:format("~s~n",[H]), halt()' \
                      -setcookie "${COOKIE}" \
                      -dist_listen false \
                      ${START_EPMD} \
                      -noshell "${NAME_TYPE}" "$id"
    fi
}

# Connect to a remote node
relx_rem_sh() {
    # Remove remote_nodename when OTP-23 is the oldest version supported by rebar3/relx.
    # sort the used erts version against 11.0 to see if it is less than 11.0 (OTP-23)
    # if it is then we must generate a node name to use for the remote node.
    # But this feature is only for short names in 23.0 (erts 11.0). It can be used
    # for long names with 23.1 (erts 11.1) and above.
    if [ "${NAME_TYPE}" = "-sname" ] && [  "11.0" = "$(printf "%s\n11.0" "${ERTS_VSN}" | sort -V | head -n1)" ] ; then

        remote_nodename="${NAME_TYPE} undefined@${RELX_HOSTNAME}"
    # if the name type is longnames then make sure this is erts 11.1+
    elif [ "${NAME_TYPE}" = "-name" ] && [  "11.1" = "$(printf "%s\n11.1" "${ERTS_VSN}" | sort -V | head -n1)" ] ; then
        remote_nodename="${NAME_TYPE} undefined@${RELX_HOSTNAME}"
    else
        # Generate a unique id used to allow multiple remsh to the same node transparently
        remote_nodename="${NAME_TYPE} remsh$(relx_gen_id)-${NAME}"
    fi

    # Get the node's ticktime so that we use the same one
    TICKTIME="$(erl_rpc net_kernel get_net_ticktime)"

    # Setup remote shell command to control node
    # -dist_listen is new in OTP-23. It keeps the remote node from binding to a listen port
    # and implies the option -hidden
    # shellcheck disable=SC2086
    exec "$BINDIR/erl" ${remote_nodename} -remsh "$NAME" -boot "$REL_DIR"/start_clean -mode interactive \
         -boot_var SYSTEM_LIB_DIR "$SYSTEM_LIB_DIR" \
         -setcookie "$COOKIE" -hidden -kernel net_ticktime "$TICKTIME" \
         -dist_listen false \
         $DIST_ARGS \
         $EXTRA_DIST_ARGS
}

erl_rpc() {
    case "$ERL_RPC" in
        "relx_nodetool")
            relx_nodetool rpc "$@"
            ;;
        *)
            command=$*

            # erl_call -R is recommended for generating dynamic node name but is only available in 23.0+
            if [  "11.0" = "$(printf "%s\n11.0" "${ERTS_VSN}" | sort -V | head -n1)" ] ; then
                DYNAMIC_NAME="-R"
            else
                DYNAMIC_NAME="-r"
            fi

            if [ "$ADDRESS" ]; then
                result=$("$ERL_RPC" "${DYNAMIC_NAME}" -c "${COOKIE}" -address "${ADDRESS}" -timeout "${RELX_RPC_TIMEOUT}" -a "${command}")
            else
                result=$("$ERL_RPC" "$NAME_TYPE" "$NAME" "${DYNAMIC_NAME}" -c "${COOKIE}" -timeout "${RELX_RPC_TIMEOUT}" -a "${command}")
            fi
            code=$?
            if [ $code -eq 0 ]; then
                echo "$result"
            else
                return $code
            fi
            ;;
    esac
}

erl_eval() {
    case "$ERL_RPC" in
        "relx_nodetool")
            relx_nodetool eval "$@"
            ;;
        *)
            command=$*

            if [ "$ERL_DIST_PORT" ]; then
                result=$(echo "${command}" | "$ERL_RPC" "${DYNAMIC_NAME}" -c "${COOKIE}" -address "${ADDRESS}" -timeout "${RELX_RPC_TIMEOUT}" -e)
            else
                result=$(echo "${command}" | "$ERL_RPC" "$NAME_TYPE" "$NAME" "${DYNAMIC_NAME}" -c "${COOKIE}" -timeout "${RELX_RPC_TIMEOUT}" -e)
            fi
            code=$?
            if [ $code -eq 0 ]; then
                echo "$result" | sed 's/^{ok, \(.*\)}$/\1/'
            else
                return $code
            fi
            ;;
    esac
}


# Generate a random id
relx_gen_id() {
    # To prevent exhaustion of atoms on target node, optionally avoid
    # generation of random node prefixes, if it is guaranteed calls
    # are entirely sequential.
    if [ -z "${NODETOOL_NODE_PREFIX}" ]; then
        dd count=1 bs=4 if=/dev/urandom 2> /dev/null | od -x  | head -n1 | awk '{print $2$3}'
    else
        echo "${NODETOOL_NODE_PREFIX}"
    fi
}

# Control a node with nodetool if erl_call isn't from OTP-23+
relx_nodetool() {
    command="$1"; shift

    # Generate a unique id used to allow multiple nodetool calls to the
    # same node transparently
    nodetool_id="maint$(relx_gen_id)-${NAME}"

    if [ -z "${START_EPMD}" ]; then
        ERL_FLAGS="${ERL_FLAGS} ${DIST_ARGS} ${EXTRA_DIST_ARGS} ${NAME_TYPE} $nodetool_id -setcookie ${COOKIE}" \
                 "$ERTS_DIR/bin/escript" \
                 "$ROOTDIR/bin/nodetool" \
                 "$NAME_TYPE" "$NAME" \
                 "$command" "$@"
    else
        # shellcheck disable=SC2086
        ERL_FLAGS="${ERL_FLAGS} ${DIST_ARGS} ${EXTRA_DIST_ARGS} ${NAME_TYPE} $nodetool_id -setcookie ${COOKIE}" \
                 "$ERTS_DIR/bin/escript" \
                 "$ROOTDIR/bin/nodetool" \
                 $START_EPMD "$NAME_TYPE" "$NAME" "$command" "$@"
    fi
}

# Run an escript in the node's environment
relx_escript() {
    scriptpath="$1"; shift
    export RELEASE_ROOT_DIR

    "$ERTS_DIR/bin/escript" "$ROOTDIR/$scriptpath" "$@"
}

# Convert {127,0,0,1} to 127.0.0.1 (inet:ntoa/1)
addr_tuple_to_str() {
    addr="$1"
    saved_IFS="$IFS"
    IFS="{,}'\" "
    # shellcheck disable=SC2086
    eval set -- $addr
    IFS="$saved_IFS"

    case $# in
    4) printf '%u.%u.%u.%u' "$@";;
    8) printf '%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x' "$@";;
    *) echo "Cannot parse IP address tuple: '$addr'" 1>&2;;
    esac
}

make_out_file_path() {
    # Use output directory provided in the RELX_OUT_FILE_PATH environment variable
    # (default to the current location of vm.args and sys.config)
    DIR=$(dirname "$1")
    [ -d "${RELX_OUT_FILE_PATH}" ] && DIR="${RELX_OUT_FILE_PATH}"
    FILE=$(basename "$1")
    IN="${DIR}/${FILE}"

    PFX=$(echo "$IN"   | awk '{sub(/\.[^.]+$/, "", $0)}1')
    SFX=$(echo "$FILE" | awk -F . '{if (NF>1) print $NF}')
    if [ "$RELX_MULTI_NODE" ]; then
        echo "${PFX}.${NAME}.${SFX}"
    else
        echo "${PFX}.${SFX}"
    fi
}

# Replace environment variables
replace_os_vars() {
    awk '{
        while(match($0,"[$]{[^}]*}")) {
            var=substr($0,RSTART+2,RLENGTH -3)
            slen=split(var,arr,":-")
            v=arr[1]
            e=ENVIRON[v]
            gsub("&","\\\\\\&",e)
            if(slen > 1 && e=="") {
                i=index(var, ":-"arr[2])
                def=substr(var,i+2)
                gsub("[$]{"var"}",def)
            } else {
                gsub("[$]{"var"}",e)
            }
        }
    }1' < "$1" > "$2"
}

add_path() {
    # Use $CWD/$1 if exists, otherwise releases/VSN/$1
    IN_FILE_PATH=$2
    if [ -z "$IN_FILE_PATH" ]; then
        if [ -f "$RELEASE_ROOT_DIR/$1" ]; then
            IN_FILE_PATH="$RELEASE_ROOT_DIR/$1"
        else
            IN_FILE_PATH="$REL_DIR/$1"
        fi
    fi
    echo "$IN_FILE_PATH"
}

check_replace_os_vars() {
    IN_FILE_PATH=$(add_path "$1" "$2")
    OUT_FILE_PATH="$IN_FILE_PATH"
    SRC_FILE_PATH="$IN_FILE_PATH.src"
    ORIG_FILE_PATH="$IN_FILE_PATH.orig"
    if [ -f "$SRC_FILE_PATH" ]; then
        OUT_FILE_PATH=$(make_out_file_path "$IN_FILE_PATH")
        replace_os_vars "$SRC_FILE_PATH" "$OUT_FILE_PATH"
    elif [ "$RELX_REPLACE_OS_VARS" ]; then
        OUT_FILE_PATH=$(make_out_file_path "$IN_FILE_PATH")
        # If vm.args.orig or sys.config.orig is present then use that
        if [ -f "$ORIG_FILE_PATH" ]; then
           IN_FILE_PATH="$ORIG_FILE_PATH"
        fi

        # apply the environment variable substitution to $IN_FILE_PATH
        # the result is saved to $OUT_FILE_PATH
        # if they are both the same, then ensure that we don't clobber
        # the file by saving a backup with the .orig extension
        if [ "$IN_FILE_PATH" = "$OUT_FILE_PATH" ]; then
            cp "$IN_FILE_PATH" "$ORIG_FILE_PATH"
            replace_os_vars "$ORIG_FILE_PATH" "$OUT_FILE_PATH"
        else
            replace_os_vars "$IN_FILE_PATH" "$OUT_FILE_PATH"
        fi
    else
        # If vm.arg.orig or sys.config.orig is present then use that
        if [ -f "$ORIG_FILE_PATH" ]; then
            OUT_FILE_PATH=$(make_out_file_path "$IN_FILE_PATH")
            cp "$ORIG_FILE_PATH" "$OUT_FILE_PATH"
        fi
    fi
    echo "$OUT_FILE_PATH"
}

relx_run_hooks() {
    HOOKS=$1
    for hook in $HOOKS
    do
        # the scripts arguments at this point are separated
        # from each other by | , we now replace these
        # by empty spaces and give them to the `set`
        # command in order to be able to extract them
        # separately
        # shellcheck disable=SC2046
        set $(echo "$hook" | sed -e 's/|/ /g')
        HOOK_SCRIPT=$1; shift
        # all hook locations are expected to be
        # relative to the start script location
        # shellcheck disable=SC1090,SC2240
        [ -f "$SCRIPT_DIR/$HOOK_SCRIPT" ] && . "$SCRIPT_DIR/$HOOK_SCRIPT" "$@"
    done
}

relx_disable_hooks() {
    PRE_START_HOOKS=""
    POST_START_HOOKS=""
    PRE_STOP_HOOKS=""
    POST_STOP_HOOKS=""
}

relx_is_extension() {
    EXTENSION=$1
    case "$EXTENSION" in
        {{{ extensions }}})
            echo "1"
        ;;
        *)
            echo "0"
        ;;
    esac
}

relx_get_extension_script() {
    EXTENSION=$1
    echo "extensions/${EXTENSION}"
}

relx_run_extension() {
    # drop the first argument which is the name of the
    # extension script
    EXTENSION_SCRIPT=$1
    shift
    # all extension script locations are expected to be
    # relative to the start script location
    # shellcheck disable=SC1090,SC2240
    [ -f "$SCRIPT_DIR/$EXTENSION_SCRIPT" ] && . "$SCRIPT_DIR/$EXTENSION_SCRIPT" "$@"
}

# given a list of arguments, identify the internal ones
#   --relx-disable-hooks
# and process them accordingly
process_internal_args() {
    for arg in "$@"
    do
      shift
      case "$arg" in
          --relx-disable-hooks)
            relx_disable_hooks
            ;;
          *)
            ;;
      esac
    done
}

# process internal arguments
process_internal_args "$@"

find_erts_dir
find_erl_call
export ROOTDIR="$RELEASE_ROOT_DIR"
export BINDIR="$ERTS_DIR/bin"
export EMU="beam"
export PROGNAME="erl"
export LD_LIBRARY_PATH="$ERTS_DIR/lib:$LD_LIBRARY_PATH"
SYSTEM_LIB_DIR="$(dirname "$ERTS_DIR")/lib"

VMARGS_PATH=$(add_path vm.args "$VMARGS_PATH")

VMARGS_PATH=$(check_replace_os_vars vm.args "$VMARGS_PATH")
RELX_CONFIG_PATH=$(check_replace_os_vars sys.config "$RELX_CONFIG_PATH")

# Check vm.args and other files referenced via -args_file parameters for:
#    - nonexisting -args_files
#    - circular dependencies of -args_files
#    - relative paths in -args_file parameters
#    - multiple/mixed occurrences of -name and -sname parameters
#    - missing -name or -sname parameters
# If all checks pass, extract the target node name
set +e
TMP_NAME_ARG=$(awk 'function shell_quote(str)
{
    gsub(/'\''/,"'\'\\\\\'\''", str);
    return "'\''" str "'\''"
}

function check_name(file)
{
    # if file exists, then it should be readable
    if (system("test -f " shell_quote(file)) == 0 && system("test -r " shell_quote(file)) != 0) {
        print file" not readable"
        exit 3
    }
    while ((getline line<file)>0) {
        if (line~/^-args_file +/) {
            gsub(/^-args_file +| *$/, "", line)
            if (line in files) {
                print "circular reference to "line" encountered in "file
                exit 5
            }
            files[line]=line
            check_name(line)
        }
        else if (line~/^-s?name +/) {
            if (name!="") {
                print "\""line"\" parameter found in "file" but already specified as \""name"\""
                exit 2
            }
            name=line
        }
    }
}

BEGIN {
    split("", files)
    name=""
}

{
    files[FILENAME]=FILENAME
    check_name(FILENAME)
    if (name=="") {
        print "need to have exactly one of either -name or -sname parameters but none found"
        exit 1
    }
    print name
    exit 0
}' "$VMARGS_PATH")
TMP_NAME_ARG_RC=$?
case $TMP_NAME_ARG_RC in
    0) NAME_ARG="$TMP_NAME_ARG";;
    *) echo "$TMP_NAME_ARG"
       exit $TMP_NAME_ARG_RC;;
esac
unset TMP_NAME_ARG
unset TMP_NAME_ARG_RC
set -e

# Perform replacement of variables in ${NAME_ARG}
NAME_ARG=$(eval echo "${NAME_ARG}")

# Extract the name type and name from the NAME_ARG for REMSH
NAME_TYPE="$(echo "$NAME_ARG" | awk '{print $1}')"
NAME="$(echo "$NAME_ARG" | awk '{print $2}')"

# Extract dist arguments
DIST_ARGS=""
PROTO_DIST="$(grep '^-proto_dist' "$VMARGS_PATH" || true)"
if [ "$PROTO_DIST" ]; then
    DIST_ARGS="${PROTO_DIST}"
fi
START_EPMD="$(grep '^-start_epmd' "$VMARGS_PATH" || true)"
if [ "$START_EPMD" ]; then
    DIST_ARGS="${DIST_ARGS} ${START_EPMD}"
fi
EPMD_MODULE="$(grep '^-epmd_module' "$VMARGS_PATH" || true)"
if [ "$EPMD_MODULE" ]; then
    DIST_ARGS="${DIST_ARGS} ${EPMD_MODULE}"
fi
INET_DIST_USE_INTERFACE="$(grep '^-kernel  *inet_dist_use_interface' "$VMARGS_PATH" || true)"
if [ "$INET_DIST_USE_INTERFACE" ]; then
    DIST_ARGS="${DIST_ARGS} ${INET_DIST_USE_INTERFACE}"
fi

if [ "$ERL_DIST_PORT" ]; then
    if [ "$INET_DIST_USE_INTERFACE" ]; then
        ADDRESS="$(addr_tuple_to_str "${INET_DIST_USE_INTERFACE#*inet_dist_use_interface }"):$ERL_DIST_PORT"
    else
        ADDRESS="$ERL_DIST_PORT"
    fi
    if [  "11.1" = "$(printf "%s\n11.1" "${ERTS_VSN}" | sort -V | head -n1)" ] ; then
        # unless set by the user, set start_epmd to false when ERL_DIST_PORT is used
        if [ ! "$START_EPMD" ]; then
            EXTRA_DIST_ARGS="-erl_epmd_port ${ERL_DIST_PORT} -start_epmd false"
        else
            EXTRA_DIST_ARGS="-erl_epmd_port ${ERL_DIST_PORT}"
        fi
    else
        ERL_DIST_PORT_WARNING="ERL_DIST_PORT is set and used to set the port, but doing so on ERTS version ${ERTS_VSN} means remsh/rpc will not work for this release"
        if ! command -v logger > /dev/null 2>&1
        then
            echo "WARNING: ${ERL_DIST_PORT_WARNING}"
        else
            logger -p warning -t "${REL_NAME}[$$]" "${ERL_DIST_PORT_WARNING}"
        fi
        EXTRA_DIST_ARGS="-kernel inet_dist_listen_min ${ERL_DIST_PORT} -kernel inet_dist_listen_max ${ERL_DIST_PORT}"
    fi
fi

# Extract the target cookie
# Do this before relx_get_nodename so we can use it and not create a ~/.erlang.cookie
if [ -n "$RELX_COOKIE" ]; then
    COOKIE="$RELX_COOKIE"
else
    COOKIE_ARG="$(grep '^-setcookie' "$VMARGS_PATH" || true)"
    DEFAULT_COOKIE_FILE="$HOME/.erlang.cookie"
    if [ -z "$COOKIE_ARG" ]; then
        if [ -f "$DEFAULT_COOKIE_FILE" ]; then
            COOKIE="$(cat "$DEFAULT_COOKIE_FILE")"
        else
            echo "No cookie is set or found. This limits the scripts functionality, installing, upgrading, rpc and getting a list of versions will not work."
        fi
    else
        # Extract cookie name from COOKIE_ARG
        COOKIE="$(echo "$COOKIE_ARG" | awk '{print $2}')"
    fi
fi

# User can specify an sname without @hostname
# This will fail when creating remote shell
# So here we check for @ and add @hostname if missing
case "${NAME}" in
    *@*) ;;                             # Nothing to do
    *)   NAME=${NAME}@$(relx_get_nodename);;  # Add @hostname
esac

# Export the variable so that it's available in the 'eval' calls
export NAME

# create a variable of just the hostname part of the nodename
RELX_HOSTNAME=$(echo "${NAME}" | cut -d'@' -f2)

test -z "$PIPE_DIR" && PIPE_BASE_DIR='/tmp/erl_pipes/'
PIPE_DIR="${PIPE_DIR:-/tmp/erl_pipes/$NAME/}"

cd "$ROOTDIR"

# Check the first argument for instructions
case "$1" in
    start|daemon|daemon_boot)
        case "$1" in
            daemon)
                shift
                START_OPTION="console"
                HEART_OPTION="daemon"
                ;;
            daemon_boot)
                shift
                START_OPTION="console_boot"
                HEART_OPTION="daemon_boot"
                ;;
            start)
                shift
                START_OPTION="console"
                HEART_OPTION="daemon"
                ;;
        esac

        ARGS="$(printf "'%s' " "$@")"

        export HEART_COMMAND="\"${RELEASE_ROOT_DIR}/bin/${REL_NAME}\" \"${HEART_OPTION}\" ${ARGS}"

        # shellcheck disable=SC2174
        test -z "$PIPE_BASE_DIR" || mkdir -m 1777 -p "$PIPE_BASE_DIR"
        mkdir -p "$PIPE_DIR"
        if [ ! -w "$PIPE_DIR" ]
        then
            echo "failed to start, user '$USER' does not have write privileges on '$PIPE_DIR', either delete it or run node as a different user"
            exit 1
        fi

        # Make sure log directory exists
        mkdir -p "$RUNNER_LOG_DIR"

        relx_run_hooks "$PRE_START_HOOKS"
        "$BINDIR/run_erl" -daemon "$PIPE_DIR" "$RUNNER_LOG_DIR" \
                          "exec \"$RELEASE_ROOT_DIR/bin/$REL_NAME\" \"$START_OPTION\" $ARGS --relx-disable-hooks"

        # wait for node to be up before running hooks
        while ! erl_rpc erlang is_alive > /dev/null 2>&1
        do
            sleep 1
        done

        relx_run_hooks "$POST_START_HOOKS"
        ;;

    stop)
        relx_run_hooks "$PRE_STOP_HOOKS"
        # Wait for the node to completely stop...
        PID="$(relx_get_pid)"
        if ! erl_rpc init stop > /dev/null 2>&1; then
            exit 1
        fi
        while kill -s 0 "$PID" 2>/dev/null;
        do
            sleep 1
        done

        # wait for node to be down before running hooks
        while erl_rpc erlang is_alive > /dev/null 2>&1
        do
            sleep 1
        done

        relx_run_hooks "$POST_STOP_HOOKS"
        ;;

    restart)
        ## Restart the VM without exiting the process
        if ! erl_rpc init restart > /dev/null; then
            exit 1
        fi
        ;;

    reboot)
        ## Restart the VM completely (uses heart to restart it)
        if ! erl_rpc init reboot > /dev/null; then
            exit 1
        fi
        ;;

    pid)
        ## Get the VM's pid
        if ! relx_get_pid; then
            exit 1
        fi
        ;;

    ping)
        ## See if the VM is alive
        ping_or_exit

        echo "pong"
        ;;

    escript)
        ## Run an escript under the node's environment
        shift
        if ! relx_escript "$@"; then
            exit 1
        fi
        ;;

    daemon_attach|attach)
        case "$1" in
            attach)
                # TODO, add here the right annoying message asking users to consider
                # instead using systemd or some such other init system
                echo "'attach' has been deprecated, replaced by 'daemon_attach' and will be removed in the short-term, please consult rebar3.org on why you should be"\
                     "using 'foreground' and an init tool such as 'systemd'"
                ;;
        esac
        # Make sure a node IS running
        ping_or_exit

        if [ ! -w "$PIPE_DIR" ]
        then
            echo "failed to attach, user '$USER' does not have sufficient privileges on '$PIPE_DIR', please run node as a different user"
            exit 1
        fi

        shift
        exec "$BINDIR/to_erl" "$PIPE_DIR"
        ;;

    remote_console|remote|remsh)
        # Make sure a node IS running
        ping_or_exit

        shift
        relx_rem_sh
        ;;

    console|console_clean|console_boot|foreground)
        FOREGROUNDOPTIONS=""
        # .boot file typically just $REL_NAME (ie, the app name)
        # however, for debugging, sometimes start_clean.boot is useful.
        # For e.g. 'setup', one may even want to name another boot script.
        case "$1" in
            console)
                if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
                  BOOTFILE="$REL_DIR/$REL_NAME"
                else
                  BOOTFILE="$REL_DIR/start"
                fi
                ;;
            foreground)
                # start up the release in the foreground for use by runit
                # or other supervision services
                if [ -f "$REL_DIR/$REL_NAME.boot" ]; then
                  BOOTFILE="$REL_DIR/$REL_NAME"
                else
                  BOOTFILE="$REL_DIR/start"
                fi
                FOREGROUNDOPTIONS="-noinput +Bd"
                ;;
            console_clean)
                # if not set by user use interactive mode for console_clean
                CODE_LOADING_MODE="${CODE_LOADING_MODE:-interactive}"
                BOOTFILE="$REL_DIR/start_clean"
                ;;
            console_boot)
                shift
                BOOTFILE="$1"
                shift
                ;;
        esac

        # if not set by user or console_clean use embedded
        CODE_LOADING_MODE="${CODE_LOADING_MODE:-embedded}"

        # Setup beam-required vars
        EMU="beam"
        PROGNAME="${0#*/}"

        export EMU
        export PROGNAME

        # Dump environment info for logging purposes
        # shellcheck disable=SC2086
        echo "Exec: $BINDIR/erlexec" $FOREGROUNDOPTIONS \
            -boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \
            -boot_var SYSTEM_LIB_DIR "$SYSTEM_LIB_DIR" \
            -config "$RELX_CONFIG_PATH" \
            -args_file "$VMARGS_PATH" \
            $EXTRA_DIST_ARGS -- "$@"
        echo "Root: $ROOTDIR"

        # Log the startup
        echo "$RELEASE_ROOT_DIR"
        if ! command -v logger > /dev/null 2>&1
        then
            echo "${REL_NAME}[$$] Starting up"
        else
            logger -t "${REL_NAME}[$$]" "Starting up"
        fi

        relx_run_hooks "$PRE_START_HOOKS"
        # Start the VM
        # The variabre FOREGROUNDOPTIONS must NOT be quoted.
        # shellcheck disable=SC2086
        exec "$BINDIR/erlexec" $FOREGROUNDOPTIONS \
            -boot "$BOOTFILE" -mode "$CODE_LOADING_MODE" \
            -boot_var SYSTEM_LIB_DIR "$SYSTEM_LIB_DIR" \
            -config "$RELX_CONFIG_PATH" \
            -args_file "$VMARGS_PATH" \
            $EXTRA_DIST_ARGS -- "$@"
        # exec will replace the current image and nothing else gets
        # executed from this point on, this explains the absence
        # of the pre start hook
        ;;
    rpc)
        # Make sure a node IS running
        ping_or_exit

        shift

        erl_rpc "$@"
        ;;
    eval)
        # Make sure a node IS running
        ping_or_exit

        shift

        erl_eval "$@"
        ;;
    help)
        if [ -z "$2" ]; then
            relx_usage
            exit 1
        fi

        TOPIC="$2"; shift
        relx_usage "$TOPIC"
        ;;
    *)
        IS_EXTENSION=$(relx_is_extension "$1")
        if [ "$IS_EXTENSION" = "1" ]; then
            EXTENSION_SCRIPT=$(relx_get_extension_script "$1")
            shift
            relx_run_extension "$EXTENSION_SCRIPT" "$@"
            # all extension scripts are expected to exit
        else
            relx_usage "$1"
        fi
        exit 1
        ;;
esac

exit 0
